<template>
  <div class="scan">
    <header>
      <p @click="closeScan">返回</p>
    </header>
    <div id="bcid">
      <div style="height:40%"></div>
      <p class="tip">...载入中...</p>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import { findByBarcode} from "@/api/goods.js";
import { findLocationByBarcode } from "@/api/Invbillmstr.js";

let scan = null;
export default {
  data() {
    return {
      codeUrl: "",
	  params: "",
      billId:"",
      recordid:""
    };
  },
  mounted() {
    //跳转时自动开启
    this.startRecognize();
  },
  methods: {
    // 创建扫描控件
    startRecognize() {
      let that = this;
      if (!window.plus) return;
      scan = new plus.barcode.Barcode(
        "bcid",
        [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13],
        {
          frameColor: "#009DE2",
          scanbarColor: "#009DE2"
        }
      );
      scan.onmarked = onmarked;
      // 开始扫描
      scan.start();
      function onmarked(type, result, file) {
        switch (type) {
          case plus.barcode.QR:
            type = "QR";
            break;
          case plus.barcode.EAN13:
            type = "EAN13";
            break;
          case plus.barcode.EAN8:
            type = "EAN8";
            break;
          default:
            type = "其它" + type;
            break;
        }
        result = result.replace(/\n/g, "");
        that.codeUrl = result;
        that.closeScan();
      }
    },
    // 关闭返回
    closeScan() {
      if (!window.plus) return;
      scan.close();
      var param=this.params;
      console.info(this.codeUrl)
      switch (param) {
          case "id":
		  findByBarcode(this.codeUrl).then(res => {
		    console.dir(res);
		    if (res.data.itemid!=null&res.data.itemid!='') {
			  this.$router.push({
			  	path: "/home/info",
			  	query:{id:res.data.itemid}
			  })
		    }
		  });
		  
            break;
          case "itemid":
            this.$router.push({
            	path: "/order/execution",
            	query:{itemid:this.codeUrl}
            })
            break;
          case "locationid":
            this.$router.push({
            	path: "/order/execution",
            	query:{locationid:this.codeUrl}
            })
            break;
        case "documentInfo":
          this.$router.push({
            path: "/order/item/info",
            query:{itemBarCode:this.codeUrl,
              billId:this.billId}
          })
          break;
        case "documentItemLocationIdEnter":
          this.$router.push({
            path: "/order/item/info/enter",
            query:{locationIdBarCode:this.codeUrl,recordid:this.recordid,status:'G'}
          })
          // findLocationByBarcode(this.codeUrl).then(res => {
          //   console.dir(res);
          //   // if (res.data.itemid!=null&res.data.itemid!='') {
          //   //   this.$router.push({
          //   //     path: "/home/info",
          //   //     query:{id:res.data.itemid}
          //   //   })
          //   // }
          //   this.$router.push({
          //     path: "/order/item/info/enter",
          //     query:{locationIdBarCode:this.codeUrl,recordid:this.recordid}
          //   })
          // });

          break;
        case "documentItemLocationIdMove":

          this.$router.push({
            path: "/order/item/info/move",
            query:{locationIdBarCode:this.codeUrl,recordid:this.recordid}
          })
          // findLocationByBarcode(this.codeUrl).then(res => {
          //   console.dir(res);
          //   // if (res.data.itemid!=null&res.data.itemid!='') {
          //   //   this.$router.push({
          //   //     path: "/home/info",
          //   //     query:{id:res.data.itemid}
          //   //   })
          //   // }
          //   this.$router.push({
          //     path: "/order/item/info/move",
          //     query:{locationIdBarCode:this.codeUrl,recordid:this.recordid}
          //   })
          // });

          break;
        case "documentItemLocationIdOut":

          this.$router.push({
            path: "/order/item/info/out",
            query:{locationIdBarCode:this.codeUrl,recordid:this.recordid}
          })
          // findLocationByBarcode(this.codeUrl).then(res => {
          //   console.dir(res);
          //   // if (res.data.itemid!=null&res.data.itemid!='') {
          //   //   this.$router.push({
          //   //     path: "/home/info",
          //   //     query:{id:res.data.itemid}
          //   //   })
          //   // }
          //   this.$router.push({
          //     path: "/order/item/info/out",
          //     query:{locationIdBarCode:this.codeUrl,recordid:this.recordid}
          //   })
          // });

          break;
        case "documentItemLocationId":
          findLocationByBarcode(this.codeUrl).then(res => {
            console.dir(res);
            // if (res.data.itemid!=null&res.data.itemid!='') {
            //   this.$router.push({
            //     path: "/home/info",
            //     query:{id:res.data.itemid}
            //   })
            // }
            this.$router.push({
              path: "/order/item/info",
              query:{locationIdBarCode:this.codeUrl,recordid:this.recordid}
            })
          });

          break;
          default:
            break;



	  }
/*      this.$router.push({ path: "/home" });
 */
    },
    scanImg() {
      // 从系统相册选择文件
      if (!window.plus) return;
      plus.gallery.pick(
        function(path) {
          plus.barcode.scan(
            path,
            function(type, result) {
              window.localStorage.codeUrl2 = result;
            },
            function(e) {
              console.log(e);
              window.localStorage.codeUrl2 = e;
              plus.nativeUI.alert("如果图片无法识别,请用扫码上传");
            }
          );
        },
        function(e) {
          alert("取消选择图片");
        },
        { filter: "image" }
      );
    }
  },created() {
				if( this.$route.query.params!=null){
					this.params = this.$route.query.params
				}
				console.info(this.$route.query.billId)
    if( this.$route.query.billId!=null){
      this.billId = this.$route.query.billId
    }
    if( this.$route.query.recordid!=null){
      this.recordid = this.$route.query.recordid
    }
            }
};
</script>
<style lang="scss">
.scan {
  height: 100%;
  #bcid {
    width: 100%;
    position: absolute;
    left: 0;
    right: 0;
    top: 1rem;
    bottom: 0;
    text-align: center;
    color: #fff;
    background: #ccc;
  }
  header {
    position: absolute;
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    color: #009de2;
    left: 0.3rem;
    top: 0;
    right: 0.3rem;
    height: 1rem;
    line-height: 1rem;
    z-index: 2;
    span {
      color: #000;
    }
  }
}
</style>
